"""AdminPermissionSeedTableSeeder Seeder."""

from masoniteorm.seeds import Seeder
from app.models.AdminPermission import AdminPermission


class AdminPermissionTableSeeder(Seeder):
    def run(self):
        """Run the database seeds."""
        print("写入 admin_permissions 数据")
        print("删除 admin_permissions 数据")
        AdminPermission.truncate()

        uri   = '#'
        pid   = 0
        path  = '0'
        index = {'title': 0, 'uri': 1, 'pid': 2, 'path': 3}

        root_permissions = [
            ['菜单', uri, pid, path],
            ['系统参数', uri, pid, path],
            ['管理员', uri, pid, path],
            ['角色', uri, pid, path],
            ['操作日志', uri, pid, path],
            ['登录日志', uri, pid, path],
            ['访问权限', uri, pid, path],
            ['角色权限', uri, pid, path],
            ['角色绑定', uri, pid, path],
        ]
        permissions = [
            ['菜单-列表', 'admin/menu/index', '菜单', path],
            ['菜单-创建', 'admin/menu/create', '菜单', path],
            ['菜单-保存', 'admin/menu/store', '菜单', path],
            ['菜单-详情', 'admin/menu/show', '菜单', path],
            ['菜单-编辑', 'admin/menu/edit', '菜单', path],
            ['菜单-更新', 'admin/menu/update', '菜单', path],
            ['菜单-删除', 'admin/menu/destroy', '菜单', path],

            ['系统参数-列表', 'admin/config/index', '系统参数', path],
            ['系统参数-编辑', 'admin/config/edit', '系统参数', path],
            ['系统参数-更新', 'admin/config/update', '系统参数', path],

            ['管理员-列表', 'admin/user/index', '管理员', path],
            ['管理员-详情', 'admin/user/show', '管理员', path],
            ['管理员-创建', 'admin/user/create', '管理员', path],
            ['管理员-编辑', 'admin/user/edit', '管理员', path],
            ['管理员-保存', 'admin/user/store', '管理员', path],
            ['管理员-更新', 'admin/user/update', '管理员', path],
            ['管理员-删除', 'admin/user/destroy', '管理员', path],

            ['角色-列表', 'admin/role/index', '角色', path],
            ['角色-详情', 'admin/role/show', '角色', path],
            ['角色-创建', 'admin/role/create', '角色', path],
            ['角色-编辑', 'admin/role/edit', '角色', path],
            ['角色-保存', 'admin/role/store', '角色', path],
            ['角色-更新', 'admin/role/update', '角色', path],
            ['角色-删除', 'admin/role/destroy', '角色', path],

            ['操作日志-列表', 'admin/action-log/index', '操作日志', path],
            ['操作日志-详情', 'admin/action-log/show', '操作日志', path],

            ['登录日志-列表', 'admin/user-login-log/index', '登录日志', path],
            ['登录日志-详情', 'admin/user-login-log/show', '登录日志', path],

            ['访问权限-列表', 'admin/permission/index', '访问权限', path],
            ['访问权限-详情', 'admin/permission/show', '访问权限', path],
            ['访问权限-创建', 'admin/permission/create', '访问权限', path],
            ['访问权限-编辑', 'admin/permission/edit', '访问权限', path],
            ['访问权限-保存', 'admin/permission/store', '访问权限', path],
            ['访问权限-更新', 'admin/permission/update', '访问权限', path],
            ['访问权限-删除', 'admin/permission/destroy', '访问权限', path],

            ['角色权限-列表', 'admin/role-permission/index', '角色权限', path],
            ['角色权限-详情', 'admin/role-permission/show', '角色权限', path],
            ['角色权限-保存', 'admin/role-permission/store', '角色权限', path],

            ['角色绑定-创建', 'admin/bind-role/create', '角色绑定', path],
            ['角色绑定-保存', 'admin/bind-role/store', '角色绑定', path],
        ]

        for permission in root_permissions:
            admin_permission = AdminPermission.where('title', permission[index['title']]).first()
            if admin_permission is not None:
                print("{0}:{1} 已存在".format(permission[index['title']], permission[index['uri']]))
                continue

            print("{0}:{1} 写入成功".format(permission[index['title']], permission[index['uri']]))
            AdminPermission.generate_log(permission[index['title']], permission[index['uri']], permission[index['pid']], permission[index['path']])

        for permission in permissions:
            admin_permission = AdminPermission.where('uri', permission[index['uri']]).first()
            if admin_permission is not None:
                print("{0}:{1} 已存在".format(permission[index['title']], permission[index['uri']]))
                continue

            pid  = 0
            path = '0'

            admin_permission2 = AdminPermission.where('title', permission[index['pid']]).where('pid', 0).first()
            if admin_permission2 is not None:
                pid  = admin_permission2.id
                path = AdminPermission.generate_path(pid)

            print("{0}:{1} 写入成功".format(permission[index['title']], permission[index['uri']]))
            AdminPermission.generate_log(permission[index['title']], permission[index['uri']], pid, path)
